package jdbcTest2;

//  修改图书《深入理解Java虚拟机》的价格为61.20

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Update {
    public static void main(String[] args) throws SQLException {
        // 创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("Xianmeng");

        // 建立连接
        Connection connection = dataSource.getConnection();

        // 构造SQL + 预处理
        Scanner in = new Scanner(System.in);
        System.out.println("请输入书名：");
        String book = in.next();
        System.out.println("请输入价格：");
        double price = in.nextDouble();

        String sql = "update book set price = ? where name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(2,book);
        statement.setDouble(1,price);
        System.out.println("statement:" +statement);

        // 执行
        int n = statement.executeUpdate();
        System.out.println("影响条数n="+n);

        // 释放
        statement.close();
        connection.close();
    }
}
